home *** CD-ROM | disk | FTP | other *** search
/ Aminet 40 / Aminet 40 (2000)(Schatztruhe)[!][Dec 2000].iso / Aminet / dev / lang / Python16_Src.lha / Python16_Source / Modules / resource.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-08-03  |  5.9 KB  |  257 lines

  1. #include "Python.h"
  2. #include "mytime.h" /* needed for SunOS4.1 */
  3. #include <sys/resource.h>
  4. #include <sys/time.h>
  5. #include <unistd.h>
  6. #include <string.h>
  7. #include <errno.h>
  8.  
  9. /* On some systems, these aren't in any header file.
  10.    On others they are, with inconsistent prototypes.
  11.    We declare the (default) return type, to shut up gcc -Wall;
  12.    but we can't declare the prototype, to avoid errors
  13.    when the header files declare it different.
  14.    Worse, on some Linuxes, getpagesize() returns a size_t... */
  15. #ifndef linux
  16. int getrusage();
  17. int getpagesize();
  18. #endif
  19.  
  20. #define doubletime(TV) ((double)(TV).tv_sec + (TV).tv_usec * 0.000001)
  21.  
  22. static PyObject *ResourceError;
  23.  
  24. static PyObject *
  25. resource_getrusage(self, args)
  26.     PyObject *self;
  27.     PyObject *args;
  28. {
  29.     int who;
  30.     struct rusage ru;
  31.  
  32.     if (!PyArg_ParseTuple(args, "i:getrusage", &who))
  33.         return NULL;
  34.  
  35.     if (getrusage(who, &ru) == -1) {
  36.         if (errno == EINVAL) {
  37.             PyErr_SetString(PyExc_ValueError,
  38.                     "invalid who parameter");
  39.             return NULL;
  40.         } 
  41.         PyErr_SetFromErrno(ResourceError);
  42.         return NULL;
  43.     }
  44.  
  45.     /* Yeah, this 16-tuple is way ugly. It's probably a lot less
  46.        ugly than a dictionary with keys (or object attributes)
  47.        named things like 'ixrss'. 
  48.        */
  49.     return Py_BuildValue(
  50.         "ddiiiiiiiiiiiiii",
  51.         doubletime(ru.ru_utime),     /* user time used */
  52.         doubletime(ru.ru_stime),     /* system time used */
  53.         ru.ru_maxrss,             /* max. resident set size */
  54.         ru.ru_ixrss,             /* shared memory size */
  55.         ru.ru_idrss,             /* unshared memory size */
  56.         ru.ru_isrss,             /* unshared stack size */
  57.         ru.ru_minflt,             /* page faults not requiring I/O*/
  58.         ru.ru_majflt,             /* page faults requiring I/O */
  59.         ru.ru_nswap,             /* number of swap outs */
  60.         ru.ru_inblock,             /* block input operations */
  61.         ru.ru_oublock,             /* block output operations */
  62.         ru.ru_msgsnd,             /* messages sent */
  63.         ru.ru_msgrcv,             /* messages received */
  64.         ru.ru_nsignals,             /* signals received */
  65.         ru.ru_nvcsw,             /* voluntary context switchs */
  66.         ru.ru_nivcsw             /* involuntary context switchs */
  67.         );
  68. }
  69.  
  70.  
  71. static PyObject *
  72. resource_getrlimit(self, args)
  73.     PyObject *self;
  74.     PyObject *args;
  75. {
  76.     struct rlimit rl;
  77.     int resource;
  78.  
  79.     if (!PyArg_ParseTuple(args, "i:getrlimit", &resource)) 
  80.         return NULL;
  81.  
  82.     if (resource < 0 || resource >= RLIM_NLIMITS) {
  83.         PyErr_SetString(PyExc_ValueError,
  84.                 "invalid resource specified");
  85.         return NULL;
  86.     }
  87.  
  88.     if (getrlimit(resource, &rl) == -1) {
  89.         PyErr_SetFromErrno(ResourceError);
  90.         return NULL;
  91.     }
  92.  
  93. #if defined(HAVE_LONG_LONG)
  94.     if (sizeof(rl.rlim_cur) > sizeof(long)) {
  95.         return Py_BuildValue("LL",
  96.                      (LONG_LONG) rl.rlim_cur,
  97.                      (LONG_LONG) rl.rlim_max);
  98.     }
  99. #endif
  100.     return Py_BuildValue("ii", (long) rl.rlim_cur, (long) rl.rlim_max);
  101. }
  102.  
  103. static PyObject *
  104. resource_setrlimit(self, args)
  105.     PyObject *self;
  106.     PyObject *args;
  107. {
  108.     struct rlimit rl;
  109.     int resource;
  110.     PyObject *curobj, *maxobj;
  111.  
  112.     if (!PyArg_ParseTuple(args, "i(OO):setrlimit", &resource, &curobj, &maxobj))
  113.         return NULL;
  114.  
  115.     if (resource < 0 || resource >= RLIM_NLIMITS) {
  116.         PyErr_SetString(PyExc_ValueError,
  117.                 "invalid resource specified");
  118.         return NULL;
  119.     }
  120.  
  121. #if !defined(HAVE_LARGEFILE_SUPPORT)
  122.     rl.rlim_cur = PyInt_AsLong(curobj);
  123.     rl.rlim_max = PyInt_AsLong(maxobj);
  124. #else
  125.     /* The limits are probably bigger than a long */
  126.     rl.rlim_cur = PyLong_Check(curobj) ?
  127.         PyLong_AsLongLong(curobj) : PyInt_AsLong(curobj);
  128.     rl.rlim_max = PyLong_Check(maxobj) ?
  129.         PyLong_AsLongLong(maxobj) : PyInt_AsLong(maxobj);
  130. #endif
  131.  
  132.     rl.rlim_cur = rl.rlim_cur & RLIM_INFINITY;
  133.     rl.rlim_max = rl.rlim_max & RLIM_INFINITY;
  134.     if (setrlimit(resource, &rl) == -1) {
  135.         if (errno == EINVAL) 
  136.             PyErr_SetString(PyExc_ValueError,
  137.                     "current limit exceeds maximum limit");
  138.         else if (errno == EPERM)
  139.             PyErr_SetString(PyExc_ValueError,
  140.                     "not allowed to raise maximum limit");
  141.         else
  142.             PyErr_SetFromErrno(ResourceError);
  143.         return NULL;
  144.     }
  145.     Py_INCREF(Py_None);
  146.     return Py_None;
  147. }
  148.  
  149. static PyObject *
  150. resource_getpagesize(self, args)
  151.     PyObject *self;
  152.     PyObject *args;
  153. {
  154.     if (!PyArg_ParseTuple(args, ":getpagesize"))
  155.         return NULL;
  156.     return Py_BuildValue("i", getpagesize());
  157. }
  158.  
  159. /* List of functions */
  160.  
  161. static struct PyMethodDef
  162. resource_methods[] = {
  163.     {"getrusage",    resource_getrusage,   1},
  164.     {"getrlimit",    resource_getrlimit,   1},
  165.     {"setrlimit",    resource_setrlimit,   1},
  166.     {"getpagesize",  resource_getpagesize, 1},
  167.     {NULL, NULL}                 /* sentinel */
  168. };
  169.  
  170.  
  171. /* Module initialization */
  172.  
  173. static void
  174. ins(PyObject *dict, char *name, int value)
  175. {
  176.     PyObject *v = PyInt_FromLong((long) value);
  177.     if (v) {
  178.         PyDict_SetItemString(dict, name, v);
  179.         Py_DECREF(v);
  180.     }
  181.     /* errors will be checked by initresource() */
  182. }
  183.  
  184. void initresource()
  185. {
  186.     PyObject *m, *d;
  187.  
  188.     /* Create the module and add the functions */
  189.     m = Py_InitModule("resource", resource_methods);
  190.  
  191.     /* Add some symbolic constants to the module */
  192.     d = PyModule_GetDict(m);
  193.     ResourceError = PyErr_NewException("resource.error", NULL, NULL);
  194.     PyDict_SetItemString(d, "error", ResourceError);
  195.  
  196.     /* insert constants */
  197. #ifdef RLIMIT_CPU
  198.     ins(d, "RLIMIT_CPU", RLIMIT_CPU);
  199. #endif
  200.  
  201. #ifdef RLIMIT_FSIZE
  202.     ins(d, "RLIMIT_FSIZE", RLIMIT_FSIZE);
  203. #endif
  204.  
  205. #ifdef RLIMIT_DATA
  206.     ins(d, "RLIMIT_DATA", RLIMIT_DATA);
  207. #endif
  208.  
  209. #ifdef RLIMIT_STACK
  210.     ins(d, "RLIMIT_STACK", RLIMIT_STACK);
  211. #endif
  212.  
  213. #ifdef RLIMIT_CORE
  214.     ins(d, "RLIMIT_CORE", RLIMIT_CORE);
  215. #endif
  216.  
  217. #ifdef RLIMIT_NOFILE
  218.     ins(d, "RLIMIT_NOFILE", RLIMIT_NOFILE);
  219. #endif
  220.  
  221. #ifdef RLIMIT_OFILE
  222.     ins(d, "RLIMIT_OFILE", RLIMIT_OFILE);
  223. #endif
  224.  
  225. #ifdef RLIMIT_VMEM
  226.     ins(d, "RLIMIT_VMEM", RLIMIT_VMEM);
  227. #endif
  228.  
  229. #ifdef RLIMIT_AS
  230.     ins(d, "RLIMIT_AS", RLIMIT_AS);
  231. #endif
  232.  
  233. #ifdef RLIMIT_RSS
  234.     ins(d, "RLIMIT_RSS", RLIMIT_RSS);
  235. #endif
  236.  
  237. #ifdef RLIMIT_NPROC
  238.     ins(d, "RLIMIT_NPROC", RLIMIT_NPROC);
  239. #endif
  240.  
  241. #ifdef RLIMIT_MEMLOCK
  242.     ins(d, "RLIMIT_MEMLOCK", RLIMIT_MEMLOCK);
  243. #endif
  244.  
  245. #ifdef RUSAGE_SELF
  246.     ins(d, "RUSAGE_SELF", RUSAGE_SELF);
  247. #endif
  248.  
  249. #ifdef RUSAGE_CHILDREN
  250.     ins(d, "RUSAGE_CHILDREN", RUSAGE_CHILDREN);
  251. #endif
  252.  
  253. #ifdef RUSAGE_BOTH
  254.     ins(d, "RUSAGE_BOTH", RUSAGE_BOTH);
  255. #endif
  256. }
  257.